<template>
    <div class="notification">
        <div class="bg">
            <div class="logo" @click="toNoticList">
                <img src="../../assets/logo.png" alt="" @click.stop="toHome">
                <div>
                    通知公告
                </div>
            </div>
        </div>
        <!-- <div class="banner">
            <el-carousel height="220px">
                <el-carousel-item>
                    <img src="../../assets/banner1.jpg" alt="">
                </el-carousel-item>
                <el-carousel-item>
                    <img src="../../assets/banner2.jpg" alt="">
                </el-carousel-item>
                <el-carousel-item>
                    <img src="../../assets/banner3.jpg" alt="">
                </el-carousel-item>
            </el-carousel>
        </div> -->
        <div class="search">
            <el-form ref="ruleForm" :model="queryParams" :inline="true" class="demo-form-inline" label-width="100px">
                <el-form-item label="关键字" prop="title">
                    <el-input v-model="queryParams.title" clearable></el-input>
                </el-form-item>
                <el-form-item label="发布部门" prop="department">
                    <el-select v-model="queryParams.department" clearable placeholder="请选择部门">
                        <el-option v-for="item in deptList" :key="item.value" :label="item.label" :value="item.label">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="开始时间" prop="startDate">
                    <el-date-picker type="date" v-model="queryParams.startDate" value-format="yyyy-MM-dd">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="结束时间" prop="endDate">
                    <el-date-picker type="date" v-model="queryParams.endDate" value-format="yyyy-MM-dd">
                    </el-date-picker>
                </el-form-item>
                <div class="search-btn">
                    <el-button type="primary" icon="el-icon-refresh" @click="resetForm('ruleForm')">重置</el-button>
                    <el-button @click="onSearch" type="primary" icon="el-icon-search">查询</el-button>
                    <el-button v-if="roleName == 'Admin'" type="primary" @click="noticMyNotic"
                        icon="el-icon-user">我发布的</el-button>
                    <el-button v-if="roleName == 'Admin'" type="primary" @click="noticAdd"
                        icon="el-icon-circle-plus-outline">新建公告</el-button>
                </div>
            </el-form>
        </div>
        <div class="content">
            <div class="left">
                <h3>通知公告</h3>
                <ul>
                    <li :class="noticIndex == index ? 'active' : ''" @click="onChangeLi(index, item)"
                        v-for="(item, index) in noticClassify">{{ reviseTypes(item) }}</li>
                </ul>
            </div>
            <div class="right">
                <div class="head">
                    {{ reviseTypes(noticTitle) }}
                </div>
                <div class="list" v-if="noticeList.length>0">
                    <div @click="toDetail(ele.id, ele.hasRead, ele.scanNum, ele.createdAt, ele.types)" class="item" v-for="ele in noticeList">
                        <span :style="{ marginLeft: ele.hasRead == 1 ? '30px' : '0' }"> <span class="dot"
                                v-if="ele.hasRead == 0"></span>{{ ele.title }}</span>
                        <span class="time">{{ ele.createdAt.slice(0, 10) }}</span>
                    </div>
                </div>
                <div class="pagination">
                    <el-pagination :background="true" @size-change="handleSizeChange"
                        @current-change="handleCurrentChange" :current-page.sync="currentPage" :page-size="10"
                        layout="total, prev, pager, next" :total="50">
                    </el-pagination>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import { conditionQueryNotice, listNoticeTypes, departmentList, noticeEdit } from "@/api/notice";
import { messagesRead, msgStatusChange } from "@/api/message";
export default {
    name: 'NotificationAnnouncement',
    data() {
        return {
            //角色
            roleName: localStorage.getItem("roleName"),
            noticIndex: 0,
            noticTitle: "全部通知",
            noticClassify: ["全部通知", "公告通知", "会议通知", "院内通知", "党字公文", "其他公文"],
            noticeList: [],
            currentPage: 1,
            deptList: [],//部门列表
            queryParams: {
                userId: localStorage.getItem("userId"),
                types: "",
                title: "",
                startDate: "",
                endDate: "",
                // department: ""
            }
        }
    },
    mounted() {
        this.getNoticList(this.queryParams);
        this.getDepartmentList();
        listNoticeTypes().then(res => {
            let obj_list = res.data;
            const types = obj_list.map(item => item.ntypeName);
            types.unshift("全部通知");
            this.noticClassify = types;
        });
    },
    methods: {
        reviseTypes(type){
            switch (type) {
                case "公告":
                    return "公告通知";
                case "院内通知":
                    return "校内通知";
                case "院字公文":
                    return "校字公文";
                default:
                    return type;
            }
        },
        //重置表单
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        toNoticList() {
            this.$router.push("/notification")
        },
        toHome() {
            this.$router.push("/teacher")
        },
        //获取部门列表
        getDepartmentList() {
            departmentList({ size: 50 }).then(res => {
                // this.deptList=res.data;
                this.deptList = res.data.map(dept => {
                    return {
                        value: dept.id,
                        label: dept.name,
                    }
                })
            })
        },
        //查询通知的已读未读状态
        // getNoticRead() {
        //     messagesRead({
        //         rcvUserId: localStorage.getItem('userId'),
        //         types: '通知公告',
        //     }).then(res => {
        //         if(res.code == 200){
        //             this.noticeList = res.data
        //         }
        //     })
        // },
        //获取通知列表
        getNoticList(query) {
            console.log("查询全部通知列表的参数", query)
            conditionQueryNotice(query).then(res => {
                this.noticeList = res.data;
                // this.getNoticRead();
                // this.getNoticRead();
            })
        },
        onSearch() {
            this.getNoticList(this.queryParams);
        },
        onChangeLi(index, item) {
            this.noticIndex = index;
            this.noticTitle = item;
            if (item != "全部通知") {
                this.queryParams.types = item;
                this.getNoticList(this.queryParams);
            } else {
                this.queryParams.types = "";
                this.getNoticList(this.queryParams);
            }
        },
        handleSizeChange() {

        },
        handleCurrentChange(val) {
            console.log(`当前页: ${val}`);
        },
        toDetail(id, hasRead, scanNum, createdDate, types) {
            if (hasRead == 0) {
                // 修改通知已读状态
                msgStatusChange({
                    msgId: id,
                    rcvUserId: localStorage.getItem("userId"),
                    types: "通知公告",
                    scanNum: scanNum + 1,
                    msgCreatedDate: createdDate,
                    secondTypes: types
                }).then(res => {
                    console.log(res)
                })
            }
            noticeEdit({
                id: id,
                scanNum: scanNum + 1
            }).then(() => { })
            this.$router.push("/noticDetail?id=" + id)
        },
        noticAdd() {
            this.$router.push("/noticAdd")
        },
        noticMyNotic() {
            this.$router.push("/myNotic")
        }
    }
}
</script>

<style lang="scss" scoped>
.notification {
    min-width: 1200px;

    .bg {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        height: 80px;
        z-index: 999;
        background: url("../../assets/notic-bg.jpg") no-repeat;
        cursor: pointer;

        .logo {
            padding: 0 40px;
            height: 100%;
            display: flex;
            align-items: center;
            
            &>div {
                color: white;
                margin-left: 10px;
                border-left: 1px solid white;
                padding: 14px 0;
                padding-left: 15px;
                img{
                    height: 50px;
                }
            }
        }
    }

    .banner {
        width: 1200px;
        height: 220px;
        margin-left: auto;
        margin-right: auto;
        margin-top: 120px;

        img {
            height: 100%;
            width: 100%;
        }
    }

    .search {
        width: 1200px;
        margin-left: auto;
        margin-right: auto;
        margin-top:100px;
        display: flex;
        flex-wrap: wrap;
        border: 1px solid rgba(0, 0, 0, .1);
        padding: 15px;
        box-sizing: border-box;

        .el-form {
            display: flex;
            flex-wrap: wrap;

            .el-form-item {
                width: 50%;
                margin-right: 0;
                display: flex;
                margin-bottom:15px;
                :deep .el-form-item__content {
                    flex: 1;
                }

                .el-select,
                .el-date-editor {
                    width: 100%;
                }
            }
        }

        .search-btn {
            text-align: right;
            display: flex;
            width: 100%;
            justify-content: flex-end;
        }
    }

    .content {
        max-height: calc(100vh - 122px);
        width: 1200px;
        margin-left: auto;
        margin-right: auto;
        margin-top: 30px;
        margin-bottom: 30px;
        display: flex;

        .left {
            background-color: #0D67BB;
            width: 260px;
            padding: 10px 0 80px 20px;

            h3 {
                color: white;
                font-size: 24px;
                text-align: center;
            }

            ul {
                background-color: white;
                position: relative;
                z-index: 100;
                background: white;
                padding-bottom: 0px;
                color: #666;
                margin-top: 15px;
                border-right: 1px solid rgba(0, 0, 0, .1);

                li {
                    list-style: none;
                    padding: 20px 20px;
                    border-bottom: 1px solid rgba(0, 0, 0, .1);
                    text-align: center;

                    &:hover,
                    &.active {
                        color: #fff;
                        border: 0px solid rgba(0, 0, 0, 0);
                        background: rgba(13, 103, 187, 0.5);
                    }
                }
            }
        }

        .right {
            flex: 1;
            margin-left: 40px;

            .head {
                padding: 0 0 10px 0px;
                overflow: hidden;
                margin-bottom:8px;
                box-sizing: border-box;
                border-bottom: 1px solid #0D67BB;
                color: #0D67BB;
                // line-height:20px;
                font-size: 20px;
                font-weight: bold;
            }

            .list {
                .item {
                    display: flex;
                    color: #333;
                    padding-bottom:8px;
                    margin-bottom: 20px;
                    transition: all .3s ease-in-out;
                    border-bottom: 1px dashed rgba(0, 0, 0, .1);

                    .dot,
                    .no-dot {
                        display: inline-block;
                        width: 5px;
                        height: 5px;
                        background-color: red;
                        border-radius: 50%;
                        margin-left: 8px;
                        margin-right: 15px;
                        vertical-align: middle;
                    }

                    .no-dot {
                        background-color: #acaebc;
                    }

                    span:first-child {
                        flex: 1;

                        img {
                            margin-right: 10px;
                            vertical-align: middle;
                        }
                    }

                    .time {
                        color: #999;
                        width: 100px;
                    }
                }
            }
        }

        .pagination {
            margin-top: 10px;

            .el-pagination {
                text-align: right;
            }
        }
    }
}
</style>